package com.sl.hx.shop.ui.mine.activity;

import android.graphics.Rect;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sl.hx.shop.R;
import com.sl.hx.shop.base.BaseActivity;
import com.sl.hx.shop.base.BasePresenter;
import com.sl.hx.shop.bean.LoginBean;
import com.sl.hx.shop.ui.mine.contacts.LoginContacts;
import com.sl.hx.shop.ui.mine.presnter.LoginPresenter;
import com.sl.hx.shop.utils.NumberUtils;
import com.sl.hx.shop.utils.SPUtils;
import com.sl.hx.shop.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.sl.hx.shop.constant.AppConfig.ID;
import static com.sl.hx.shop.constant.AppConfig.PHONE;

//登录页
public class LoginActivity extends BaseActivity<LoginContacts.view,LoginPresenter>implements LoginContacts.view {

    @BindView(R.id.login_bg_logo)
    ImageView loginBgLogo;
    @BindView(R.id.login_icon_close)
    ImageView login_icon_close;
    @BindView(R.id.login_user_image)
    ImageView loginUserImage;
    @BindView(R.id.login_useredit)
    EditText loginUseredit;
    @BindView(R.id.login_pwdedit)
    EditText loginPwdedit;
    @BindView(R.id.rl_edit)
    RelativeLayout rlEdit;
    @BindView(R.id.rootview)
    RelativeLayout mRootView;
    @BindView(R.id.yzm)
    TextView mCode;

    //设置一个默认值
    private int recordVisibleRec = 0;
    private String mPhone;
    private String mYzm;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter(this);
    }

    @Override
    protected void initView() {
        //防止软键盘挡住输入框
        controlKeyboardLayout(mRootView, rlEdit);

        initEdit();
    }

    private void initEdit() {
        loginUseredit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                //获取焦点
                if (b){
                    login_icon_close.setVisibility(View.VISIBLE);
                }else {
                    login_icon_close.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.back, R.id.login_icon_close, R.id.yzm, R.id.login_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.login_icon_close:
                finish();
                break;
            case R.id.yzm:
                mPhone = loginUseredit.getText().toString();
                if (TextUtils.isEmpty(mPhone)) {
                    ToastUtil.showShort(mContext, "手机号不能为空，请重新输入");
                } else if (!NumberUtils.isPhoneNum(mPhone)) {
                    ToastUtil.showShort(mContext, "请输入正确的手机号");
                }else {
                    //获取验证码
                    mPresenter.getCode(mCode,mPhone);
                }
                break;
            case R.id.login_btn:
                mPhone = loginUseredit.getText().toString();
                mYzm = loginPwdedit.getText().toString();
                if (TextUtils.isEmpty(mPhone)) {
                    ToastUtil.showShort(mContext, "手机号不能为空，请重新输入");
                } else if (!NumberUtils.isPhoneNum(mPhone)) {
                    ToastUtil.showShort(mContext, "请输入正确的手机号");
                } else if (TextUtils.isEmpty(mYzm)) {
                    ToastUtil.showShort(mContext, "验证码不能为空，请重新输入");
                } else {
                    mPresenter.login(mPhone, mYzm);
                }
                break;
        }
    }


    @Override
    public void showtoast(String s) {
        ToastUtil.showShort(mContext, s + "");
    }

    @Override
    public void loginSuccess(LoginBean loginBean, String mPhone) {
        ToastUtil.showShort(mContext,"登录成功");
         //保存id和手机号
        SPUtils.getInstance().put(ID,loginBean.getId());
        //Log.e("logoid",loginBean.getId());
        SPUtils.getInstance().put(PHONE,mPhone);
        //结束当前页面
        finish();
    }

    /**
     * @param root         最外层布局，需要调整的布局
     * @param scrollToView 被键盘遮挡的scrollToView，滚动root,使scrollToView在root可视区域的底部
     */
    private void controlKeyboardLayout(final View root, final View scrollToView) {
        root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect rect = new Rect();
                //获取root在窗体的可视区域
                root.getWindowVisibleDisplayFrame(rect);
                //获取root在窗体的不可视区域高度(被其他View遮挡的区域高度)
                int rootInvisibleHeight = root.getRootView().getHeight() - rect.bottom;
                if (Math.abs(rootInvisibleHeight - recordVisibleRec) > 200) {
                    //若不可视区域高度大于200，则键盘显示
                    if (rootInvisibleHeight > 200) {
                        int[] location = new int[2];
                        //获取scrollToView在窗体的坐标
                        scrollToView.getLocationInWindow(location);
                        //计算root滚动高度，使scrollToView在可见区域
                        int srollHeight = (location[1] + scrollToView.getHeight()) - rect.bottom;
                        srollHeight = srollHeight < 0 ? 0 : srollHeight;
                        root.scrollTo(0, srollHeight);
                    } else {
                        //键盘隐藏
                        root.scrollTo(0, 0);
                    }
                }
                recordVisibleRec = rootInvisibleHeight;
            }
        });
    }

}
